//
//  EPERViewController.swift
//  map
//
//  Created by 季晓侠 on 16/3/2.
//  Copyright © 2016年 季晓侠. All rights reserved.
//

import UIKit
import SnapKit


class EPERViewController: UIViewController,BMKLocationServiceDelegate,BMKMapViewDelegate {
     var _mapView: BMKMapView?
     var _LocationService: BMKLocationService?
     var pointAnnotation: XXJAnnotation?
   

    override func loadView() {
        view = UIView()
       
       
        navigationItem.leftBarButtonItem = UIBarButtonItem(
            image: UIImage(named: "account_icon_up"),
            style: .Plain,
            target: self,
            action: "clickLeftButton")
        navigationItem.rightBarButtonItem = UIBarButtonItem(
            image: UIImage(named: "navigationbar_pop_os7"),
            style: .Plain,
            target: self,
            action: "clickRightButton")
        _mapView = BMKMapView()
     
        
        self.view.addSubview(_mapView!)
      
        //自定义布局 mapView
        _mapView?.snp_makeConstraints(closure: { (make) -> Void in
            make.centerX.equalTo(self.view.snp_centerX)
            make.centerY.equalTo(self.view.snp_centerY)
            make.size.equalTo(self.view.snp_size)
            
        })

   

    }
   

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.orangeColor()
        startLocation()
        


    }
    override func viewWillAppear(animated: Bool) {
         _mapView!.viewWillAppear()
         _mapView!.delegate = self
       // 此处记得不用的时候需要置nil，否则影响内存的释放
        _LocationService!.delegate = self
        setAnnotation()
    }
    
    
    override func viewWillDisappear(animated: Bool) {
        _mapView!.viewWillDisappear()
        _mapView!.delegate = nil  // 不用时，置nil
        _LocationService!.delegate = nil
    }
  
    //MARK: - 开始定位
    func startLocation(){
         _LocationService?.distanceFilter = 1
        
        //初始化BMKLocationService
        _LocationService = BMKLocationService()
        //启动LocationService
        _LocationService!.startUserLocationService()
        _mapView!.showsUserLocation = false
        //设置位置跟踪态
        _mapView!.userTrackingMode = BMKUserTrackingModeFollow
        //显示定位图层
        _mapView!.showsUserLocation = true
       
        
    }
    //设置大头针
    func setAnnotation(){
        if pointAnnotation == nil{
        pointAnnotation = XXJAnnotation()
        
        var coor = CLLocationCoordinate2D()
        
        
        
        coor.latitude = 30.9605470000;
        
        coor.longitude = 121.6339810000;
        
       pointAnnotation!.coordinate = coor
        
       pointAnnotation!.title = ""
      // pointAnnotation?.type = 1
        
        _mapView?.addAnnotation(pointAnnotation)
        
        }
    }
    
    func mapView(mapView: BMKMapView!, viewForAnnotation annotation: BMKAnnotation!) -> BMKAnnotationView! {
        if (annotation as! XXJAnnotation) == pointAnnotation {
            let AnnotationViewID = "renameMark"
            var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(AnnotationViewID) as! XXJAnnotationView?
            if annotationView == nil {
                annotationView = XXJAnnotationView(annotation: annotation, reuseIdentifier: AnnotationViewID)
                // 设置颜色
//                let lable = UILabel()
//                lable.text = "1fdjaojfaiggjaoijfoaid"
//                annotationView?.addSubview(lable)
                //annotationView?.image = UIImage(named: "icon_green_bg")
                
                
                // 设置可拖曳
                //annotationView!.draggable = true
            }
            annotationView?.annotation = annotation
            return annotationView
        }
        
        return nil
    }
    //实现相关delegate 处理位置信息更新
    //处理方向变更信息
    func didUpdateUserHeading(userLocation: BMKUserLocation!) {
        _mapView!.updateLocationData(userLocation)
        
    }
    
    //处理位置坐标更新
    func didUpdateBMKUserLocation(userLocation: BMKUserLocation!) {
        _mapView!.updateLocationData(userLocation)
        //设置显示范围
        var region:BMKCoordinateRegion  = _mapView!.region;
        region.span.latitudeDelta = 0.01;
        region.span.longitudeDelta = 0.01;
        _mapView!.region = region;
    }

    
    /**
     *当mapView新添加annotation views时，调用此接口
     *@param mapView 地图View
     *@param views 新添加的annotation views
     */
    func mapView(mapView: BMKMapView!, didAddAnnotationViews views: [AnyObject]!) {
        NSLog("didAddAnnotationViews")
        
    
    }
    
   
    
    
    

//MARK:-私有控件
   // lazy var
    
//MARK: - 按钮点击方法
    
    func clickLeftButton(){
    
    
    }
    func clickRightButton(){
  
    }
}


//MARK: - 设置界面
extension EPERViewController{
    private func setupEPERView(){
    
    
    
    
    
    }






}